Solving 10385 - Duathlon (Ternary search)
[andmenj-acm.git] / 11657 - Rational Billiard / 11657.cpp
blob9071af42c0337ccb5b8c81c4a6a22e3a9ed1f2aa
1 using namespace std;
2 #include <algorithm>
3 #include <iostream>
4 #include <iterator>
5 #include <numeric>
6 #include <sstream>
7 #include <fstream>
8 #include <cassert>
9 #include <climits>
10 #include <cstdlib>
11 #include <cstring>
12 #include <string>
13 #include <cstdio>
14 #include <vector>
15 #include <cmath>
16 #include <queue>
17 #include <deque>
18 #include <stack>
19 #include <list>
20 #include <map>
21 #include <set>
23 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
24 #define For(i, a, b) for (int i=(a); i<(b); ++i)
25 #define D(x) cout << #x " is " << x << endl
27 bool boom(int m, int n, int x0, int y0, int x1, int y1, int p, int q) {
28 if (p == 0 or q == 0) {
29 int c = p * (y1 - y0) - q * (x1 - x0);
30 return (c == 0);
32 x0 *= abs(q), x1 *= abs(q), m *= abs(q);
33 y0 *= abs(p), y1 *= abs(p), n *= abs(p);
34 p /= abs(p);
35 q /= abs(q);
37 int d1 = p * (y1 - y0) - q * (x1 - x0);
38 int d2 = p * (2*n - y1 - y0) - q * (x1 - x0);
39 int d3 = p * (y1 - y0) - q * (2*m - x1 - x0);
40 int d4 = p * (2*n - y1 - y0) - q * (2*m - x1 - x0);
42 int g = __gcd(2*n*q, 2*m*p);
44 if (d1 % g == 0) return true;
45 if (d2 % g == 0) return true;
46 if (d3 % g == 0) return true;
47 if (d4 % g == 0) return true;
48 return false;
51 int main(){
52 int m, n, x0, y0, x1, y1, p, q;
53 while (cin >> m >> n >> x0 >> y0 >> x1 >> y1 >> p >> q) {
54 if (m == 0 and n == 0 and x0 == 0 and y0 == 0 and x1 == 0 and y1 == 0 and p == 0 and q == 0) break;
55 if (boom(m, n, x0, y0, x1, y1, p, q)) {
56 puts("HIT");
57 } else {
58 puts("MISS");
61 return 0;